﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class PersController : Controller
    {
        //
        // GET: /Pers/
        public JsonResult GetTenNgachTheoMaNgach()
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from mn in context.DSMaNgach
                            join nn in context.DSNhomNgach on mn.IDNhomNgach equals nn.ID 
                            select new { nn.NhomNgach, mn.MaNgach,TenNgach = mn.TenNgach, nn.SoBac, nn.HeSoGoc }).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 //customer.ID,
                                customer.TenNgach,
                                 customer.NhomNgach,
                                 customer.MaNgach,
                                 customer.SoBac,
                                 customer.HeSoGoc
                                 
                             });
            return Json(customers, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetThongTinCanBo(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var dbResult = context.GetThongTinCanBo(id).ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.Image,
                                 customer.SoHieuCBCC,
                                 customer.Ho,
                                 customer.Ten,
                                 customer.TenPhongBan,
                                 customer.TenChucVu,
                                 NgaySinh = String.Format("{0:dd/MM/yyyy}", customer.NgaySinh),
                                 customer.TrangThai,
                                 customer.TrinhDoLyLuan,
                                 customer.TrinhDoNgoaiNgu,
                                 customer.TrinhDoTinHoc,
                                 customer.TrinhDoQLNN,
                                 customer.TrinhDoChuyenMon,
                                 customer.HoKhauThuongTru,
                                 customer.DiaChi,
                                 customer.BacLuong,
                                 customer.HeSoLuong,
                                 customer.NgayTuyenDung,
                                 customer.NgayVaoDang,
                                 customer.NgayVaoChinhThuc,
                                 customer.NgayVaoDoan,
                                 customer.MaNgachCC,
                                 customer.TenNgach,
                                 
                                 
                             });
            return Json(customers, JsonRequestBehavior.AllowGet);
        }

        //get danh sach can bo theo phong
        public JsonResult GetListPersonsInDepartment(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            List<int> arr = PhongBanModels.GetAllIDToRootInDepartment(id);

             var dbResult = (from cb in context.DSCanBo
                            join cv in context.DSChucVu on cb.ChucVuHienTai equals cv.ID
                            where arr.Contains(cb.IDPhongBan)
                            //select cb).ToList();
                            select new { cb.ID,cb.SoHieuCBCC, cb.Ho, cb.Ten,cb.GioiTinh,cb.HoKhauThuongTru,
                                cb.DiaChi,TenChucVuHienTai = cv.Ten,cb.NgaySinh, cb.IDPhongBan } into abc
                             orderby abc.IDPhongBan ascending, abc.SoHieuCBCC ascending
                                select abc).ToList();
            //var dbResult = db.Customer
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.SoHieuCBCC,
                                 customer.Ho,
                                 customer.Ten,
                                 customer.TenChucVuHienTai,
                                 customer.NgaySinh,
                                 customer.GioiTinh,
                                 customer.HoKhauThuongTru,
                                 customer.DiaChi
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetListPersonsInDepartmentInclude(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            List<int> arr = PhongBanModels.GetAllIDToRootInDepartmentInclude(id);

            var dbResult = (from cb in context.DSCanBo
                            join cv in context.DSChucVu on cb.ChucVuHienTai equals cv.ID
                            where arr.Contains(cb.IDPhongBan)
                            //select cb).ToList();
                            select new
                            {
                                cb.ID,
                                cb.SoHieuCBCC,
                                cb.Ho,
                                cb.Ten,
                                cb.GioiTinh,
                                cb.HoKhauThuongTru,
                                cb.DiaChi,
                                TenChucVuHienTai = cv.Ten,
                                cb.NgaySinh,
                                cb.IDPhongBan
                            } into abc
                            orderby abc.IDPhongBan ascending, abc.SoHieuCBCC ascending
                            select abc).ToList();
            //var dbResult = db.Customer
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.SoHieuCBCC,
                                 customer.Ho,
                                 customer.Ten,
                                 customer.TenChucVuHienTai,
                                 customer.NgaySinh,
                                 customer.GioiTinh,
                                 customer.HoKhauThuongTru,
                                 customer.DiaChi
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index(int id=0)
        {
            List<CanBo> cb = CanBoModels.GetDSCanBoOfPhongBan(id);
            ViewBag.DSCanBo = cb;
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(id);
            ViewBag.PhongBan = pb;
            return View(cb);
        }
        public ActionResult List(int id = 0)
        {
            
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(id);
            ViewBag.PhongBan = pb;
            return View();
        }
        public ActionResult Add(int id)
        {
            List<DanToc> DSDT = DanTocModels.GetDSDanToc();
            ViewBag.DSDanToc = DSDT;
            List<TonGiao> DSTG = TonGiaoModels.GetDSTonGiao();
            ViewBag.DSTonGiao = DSTG;
            List<ChucVu> cv = PosModels.GetDSChucVu();
            ViewBag.DSChucVu = cv;
            
            List<ChuyenMon> cm = ChuyenMonModels.GetDSChuyenMon();
            ViewBag.DSChuyenMon = cm;
            List<QLNhaNuoc> qlnn = QLNNModels.GetDSQLNN();
            ViewBag.DSQLNhaNuoc = qlnn;
            List<LyLuan> ll = LyLuanModels.GetDSLyLuan();
            ViewBag.DSLyLuan = ll;
            List<CapNgoaiNgu> tdnn = CapNgoaiNguModels.GetDSCapNgoaiNgu();
            ViewBag.CapNgoaiNgu = tdnn;
            List<CapTinHoc> tdth = CapTinHocModels.GetDSCapTinHoc();
            ViewBag.CapTinHoc = tdth;
            List<DaoTao> daotao = DaoTaoModels.GetDSQuaTrinhDaoTao();
            ViewBag.DaoTao = daotao;
            List<LoaiHinhDaoTao> loaihinhdaotao = LoaiHinhDaoTaoModels.GetDSLoaiHinhDaoTao();
            ViewBag.LoaiHinhDaoTao = loaihinhdaotao;

            ViewBag.Id = id;
            return View();
        }
        [HttpPost]
        public ActionResult Add(FormCollection form,int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            CanBo cb = new CanBo();
            cb.SoHieuCBCC = form["SoHieuCBCC"]; cb.Ho = form["Ho"]; cb.Ten = form["Ten"]; cb.TenGoiKhac = form["TenGoiKhac"];
            cb.Image = CommonModels.XuLyDuongDanHinhAnh(form["Image"]);
            cb.NgaySinh = DateTime.Parse((form["NgaySinhCBCC"] == "" ? "1/1/1900" : form["NgaySinhCBCC"]),ci.DateTimeFormat);
            cb.GioiTinh = form["GioiTinh"]; 
            cb.NoiSinh = form["NoiSinh"]; 
            cb.QueQuan = form["QueQuan"];
            cb.IDDanToc = Convert.ToInt32(form["IDDanToc"]);
            //end tab1
            cb.IDTonGiao = Convert.ToInt32(form["IDTonGiao"]); 
            cb.HoKhauThuongTru = form["HoKhauThuongTru"]; 
            cb.DiaChi = form["DiaChi"];
            cb.TrangThai = Convert.ToInt32(form["TrangThai"]); 
            cb.NNTuyenDung = form["NNTuyenDung"];
            cb.NgayTuyenDung = DateTime.Parse((form["NgayTuyenDung"] == "" ? "1/1/1900" : form["NgayTuyenDung"]),ci.DateTimeFormat);
            cb.HinhThucTuyenDung = form["HinhThucTuyenDung"]; 
            cb.CoQuanTuyenDung = form["CoQuanTuyenDung"]; 
            
            cb.CongViecDuocGiao = form["CongViecDuocGiao"];
            //end tab2

            
            cb.TrinhDoPhoThong = form["TrinhDoPhoThong"]; 
            //Phidel
            //cb.TrinhDoChuyenMon = Convert.ToInt32(form["TrinhDoChuyenMon"]); 
            //cb.ChuyenNganh = form["ChuyenNganh"];
            //cb.TrinhDoQLNN = Convert.ToInt32(form["TrinhDoQLNN"]); 
            //cb.TrinhDoLyLuan = Convert.ToInt32(form["TrinhDoLyLuan"]);
            //end tab3

            
            if (form["NgayVaoDang"] == "")
            {
                cb.NgayVaoDang.Equals("01/01/1900");
            }
            else
            {
                cb.NgayVaoDang = DateTime.Parse(form["NgayVaoDang"],ci.DateTimeFormat);
            }

            if (form["NgayVaoChinhThuc"] == "")
            {
                cb.NgayVaoChinhThuc.Equals("01/01/1900");
            }
            else
            {
                cb.NgayVaoChinhThuc = DateTime.Parse(form["NgayVaoChinhThuc"],ci.DateTimeFormat);
            }

            if (form["NgayTGTC_CTXH"] == "")
            {
                cb.NgayTGTC_CTXH.Equals("01/01/1900");
            }
            else
            {
                cb.NgayTGTC_CTXH = DateTime.Parse(form["NgayTGTC_CTXH"],ci.DateTimeFormat);
            }

            cb.NhiemVuTrongToChuc = form["NhiemVuTrongToChuc"]; cb.QuanHam = form["QuanHam"];
            if (form["NgayNhapNgu"] == "")
            {
                cb.NgayNhapNgu.Equals("01/01/1900");
            }
            else
            {
                cb.NgayNhapNgu = DateTime.Parse(form["NgayNhapNgu"],ci.DateTimeFormat);
            }
            if (form["NgayXuatNgu"] == "")
            {
                cb.NgayXuatNgu.Equals("01/01/1900");
            }
            else
            {
                cb.NgayXuatNgu = DateTime.Parse(form["NgayXuatNgu"],ci.DateTimeFormat);
            }
            //end tab4
            cb.DanhHieu = form["DanhHieu"]; 
            cb.NamPhongTang = form["NamPhongTang"]; 
            cb.SoTruongCongTac = form["SoTruongCongTac"];
            cb.TinhTrangSucKhoe = form["TinhTrangSucKhoe"]; 
            cb.ChieuCao = form["ChieuCao"]; 
            cb.CanNang = form["CanNang"];
            cb.NhomMau = form["NhomMau"]; 
            cb.ThuongBinh = form["ThuongBinh"]; 
            cb.GiaDinhChinhSach = form["GiaDinhChinhSach"]; 
            cb.SoCMND = form["SoCMND"];
            //end tab5
            cb.SoSoBHXH = form["SoSoBHXH"]; 
            cb.Tgiacapuyxa = form["Tgiacapuyxa"]; 
            cb.TgiaHDND = form["TgiaHDND"];
            cb.NgayCap = DateTime.Parse((form["NgayCap"] == "" ? "1/1/1900" : form["NgayCap"]),ci.DateTimeFormat); 
            cb.NhanXet = form["NhanXet"]; 
            cb.LoaiHinhDaoTao = Convert.ToInt32(form["LoaiHinhDaoTao"]);
            if (form["NgayNhanChuc"] == "")
            {
                cb.NgayNhanChuc.Equals("01/01/1900");
            }
            else
            {
                cb.NgayNhanChuc = DateTime.Parse(form["NgayNhanChuc"],ci.DateTimeFormat);
            }
            cb.TiengDanToc = form["TiengDanToc"]; 
            cb.TgiaQuocPhong = form["TgiaQuocPhong"]; 
            cb.ChucVuKiemNhiem = form["ChucVuKiemNhiem"];

            cb.IDPhongBan = id;
            //themQHGD.IDCanBo = id;
            cb.CongViecDuocGiao = form["CongViecDuocGiao"];
            int idNew = CanBoModels.Add(cb);
            //THÊM MỚI VÀO CÁC BẢNG CÓ LIÊN QUAN
            //Them Trình độ chuyên môn TDSpe
            TrinhDoChuyenMon cb_chuyenmon = new TrinhDoChuyenMon();
            cb_chuyenmon.IDCanBo = idNew;
            cb_chuyenmon.IDChuyenMon = Convert.ToInt32(form["TrinhDoChuyenMon"]);
            cb_chuyenmon.ChuyenNganh = form["ChuyenNganh"];
            cb_chuyenmon.IsThongKe = true;
            TrinhDoChuyenMonModels.Add(cb_chuyenmon);

            //Them Trình độ Lý luận
            if (Convert.ToInt32(form["TrinhDoLyLuan"])>0)
            {
                TrinhDoLyLuan cb_lyluan = new TrinhDoLyLuan();
                cb_lyluan.IDCanBo = idNew;
                cb_lyluan.IDLyLuan = Convert.ToInt32(form["TrinhDoLyLuan"]);
                TrinhDoLyLuanModels.Add(cb_lyluan);  
            }

            //Them Trinh Do QLNN
            if (Convert.ToInt32(form["TrinhDoQLNN"]) > 0)
            {
                TrinhDoQLNN cb_qlnn = new TrinhDoQLNN();
                cb_qlnn.IDCanBo = idNew;
                cb_qlnn.IDQLNhaNuoc = Convert.ToInt32(form["TrinhDoQLNN"]);
                TrinhDoQLNNModels.Add(cb_qlnn);
            }

            //Thêm Trình độ ngoại ngữ
            if (Convert.ToInt32(form["TrinhDoNgoaiNgu"]) > 0)
            {
                TrinhDoNgoaiNgu cb_tdngoaingu = new TrinhDoNgoaiNgu();
                cb_tdngoaingu.IDCanBo = idNew;
                cb_tdngoaingu.STT = 1;
                cb_tdngoaingu.Ten = form["TenNgoaiNgu"];
                //cb_tdngoaingu.TrinhDo
                cb_tdngoaingu.IDTrinhDo = Convert.ToInt32(form["TrinhDoNgoaiNgu"]);
                cb_tdngoaingu.IsThongKe = true;
                TrinhDoNgoaiNguModels.Add(cb_tdngoaingu);
            }

            //Thêm Trình độ Tin hoc
            if (Convert.ToInt32(form["TrinhDoTinHoc"]) > 0)
            {
                TrinhDoTinHoc cb_tdtinhoc = new TrinhDoTinHoc();
                cb_tdtinhoc.IDCanBo = idNew;
                cb_tdtinhoc.STT = 1;
                cb_tdtinhoc.IDTrinhDo = Convert.ToInt32(form["TrinhDoTinHoc"]);
                cb_tdtinhoc.IsThongKe = true;
                TrinhDoTinHocModels.Add(cb_tdtinhoc);
            }

            //Them Quá trình công tác (lấy từ ngày lớn nhất)
            cb.ChucVuHienTai = Convert.ToInt32(form["ChucVuHienTai"]); 
            cb.MaNgachCC = form["MaNgachCC"];
            cb.TenNgachCC = form["TenNgachCC"];
            cb.BacLuong = Convert.ToInt32(form["BacLuongCBCC"] == "" ? "1" : form["BacLuongCBCC"]);
            cb.HeSoLuong = Convert.ToDouble(form["HeSoLuongCBCC"] == "" ? "0" : form["HeSoLuongCBCC"]);
            cb.NgayHuongLuong = DateTime.Parse((form["NgayHuongLuong"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["NgayHuongLuong"]), ci.DateTimeFormat);
            //Thông tin nhóm ngach của mã ngạch
            

            QTCongTac cb_qtCongTac = new QTCongTac();
            cb_qtCongTac.IDCanBo = idNew;
            cb_qtCongTac.IDChucVu = Convert.ToInt32(form["ChucVuHienTai"]);
            cb_qtCongTac.TuNgay = DateTime.Parse((form["NgayHuongLuong"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["NgayHuongLuong"]), ci.DateTimeFormat);
            //xu lý tang ngay nang luong
            BangMaNgach ma_ngach = MaNgachModels.GetChiTietMaNgachMaNgach(form["MaNgachCC"].ToString().Trim());
            BangNhomNgach nhom_ngach = NhomNgachModels.GetChiTietNhomNgach(ma_ngach.IDNhomNgach);
            int dday = DateTime.Parse((form["NgayHuongLuong"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["NgayHuongLuong"]), ci.DateTimeFormat).Day;
            int mmon = DateTime.Parse((form["NgayHuongLuong"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["NgayHuongLuong"]), ci.DateTimeFormat).Month;
            int yyear = DateTime.Parse((form["NgayHuongLuong"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["NgayHuongLuong"]), ci.DateTimeFormat).Year;
            yyear += (int)nhom_ngach.SoNamTangLuong;
            DateTime _dt = new DateTime(yyear, mmon, dday);

            cb_qtCongTac.DenNgay = DateTime.Parse(_dt.ToString("dd/MM/yyyy"), ci.DateTimeFormat);
            cb_qtCongTac.IDMaNgach = ma_ngach.ID;
            cb_qtCongTac.BacLuong = Convert.ToInt32(form["BacLuongCBCC"] == "" ? "1" : form["BacLuongCBCC"]);
            cb_qtCongTac.HeSoLuongTong = nhom_ngach.HeSoGoc;
            cb_qtCongTac.IsThongKe = true;

            QuaTrinhCongTacModels.Add(cb_qtCongTac);
            
            //xu ly dien bien luong
            tblDienBienLuong cb_dbLuong = new tblDienBienLuong();
            cb_dbLuong.IDCanBo = idNew;
            cb_dbLuong.IDChucVu = Convert.ToInt32(form["ChucVuHienTai"]);
            cb_dbLuong.TuNgay = DateTime.Parse((form["NgayHuongLuong"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["NgayHuongLuong"]), ci.DateTimeFormat);
            cb_dbLuong.DenNgay = DateTime.Parse(_dt.ToString("dd/MM/yyyy"), ci.DateTimeFormat);
            cb_dbLuong.IDMaNgach = ma_ngach.ID;
            cb_dbLuong.BacLuong = Convert.ToInt32(form["BacLuongCBCC"] == "" ? "1" : form["BacLuongCBCC"]);
            cb_dbLuong.HeSoLuong = nhom_ngach.HeSoGoc;
            cb_dbLuong.HeSoLuongTong = (nhom_ngach.HeSoGoc + ((Convert.ToInt32(form["BacLuong"] == "" ? "1" : form["BacLuong"])-1)*nhom_ngach.HeSoTang));
            cb_dbLuong.NgayTangLuong = DateTime.Parse(_dt.ToString("dd/MM/yyyy"), ci.DateTimeFormat);

            DienBienLuongModels.Add(cb_dbLuong);

            //Kiểm tra chức vụ có phụ cấp hay không->thêm vào bảng ChiTietPhuCap
            ChucVu cv = PosModels.GetChiTietDSChucVu(Convert.ToInt32(form["ChucVuHienTai"]));
            if (cv.IDPhuCap != 0 && cv.IDPhuCap != null && cv.IDPhuCap.ToString() != "" && cv.IDPhuCap.ToString() != "NULL")
            {
                ChiTietPhuCap cb_chitietphucap = new ChiTietPhuCap();
                cb_chitietphucap.IDCanBo = idNew;
                cb_chitietphucap.IDPhuCap = (int)cv.IDPhuCap;
                ChiTietPhuCapModels.Add(cb_chitietphucap);
            }
            
            return RedirectToAction("Index", "Pers", new { id = cb.IDPhongBan });
        }
        public ActionResult Update(int id)
        {
            List<DanToc> DSDT = DanTocModels.GetDSDanToc();
            ViewBag.DSDanToc = DSDT;
            List<TonGiao> DSTG = TonGiaoModels.GetDSTonGiao();
            ViewBag.DSTonGiao = DSTG;
            List<ChucVu> cv = PosModels.GetDSChucVu();
            ViewBag.DSChucVu = cv;
            
            List<ChuyenMon> cm = ChuyenMonModels.GetDSChuyenMon();
            ViewBag.DSChuyenMon = cm;
            List<QLNhaNuoc> qlnn = QLNNModels.GetDSQLNN();
            ViewBag.DSQLNhaNuoc = qlnn;
            List<LyLuan> ll = LyLuanModels.GetDSLyLuan();
            ViewBag.DSLyLuan = ll;
            List<CapNgoaiNgu> tdnn = CapNgoaiNguModels.GetDSCapNgoaiNgu();
            ViewBag.CapNgoaiNgu = tdnn;
            List<CapTinHoc> tdth = CapTinHocModels.GetDSCapTinHoc();
            ViewBag.CapTinHoc = tdth;
            List<DaoTao> daotao = DaoTaoModels.GetDSQuaTrinhDaoTao();
            ViewBag.DaoTao = daotao;
            List<LoaiHinhDaoTao> loaihinhdaotao = LoaiHinhDaoTaoModels.GetDSLoaiHinhDaoTao();
            ViewBag.LoaiHinhDaoTao = loaihinhdaotao;

            CanBo listcb = CanBoModels.GetChiTietCanBo(id);
            ViewBag.DSCanBo = listcb;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection form, int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            CanBo cb = CanBoModels.GetChiTietCanBo(id);
            cb.SoHieuCBCC = form["SoHieuCBCC"]; cb.Ho = form["Ho"]; cb.Ten = form["Ten"]; cb.TenGoiKhac = form["TenGoiKhac"];
            cb.Image = CommonModels.XuLyDuongDanHinhAnh(form["Image"]);
            cb.NgaySinh = DateTime.Parse((form["NgaySinhCBCC"] == "" ? "1/1/1900" : form["NgaySinhCBCC"]),ci.DateTimeFormat);
            cb.GioiTinh = form["GioiTinh"];
            cb.NoiSinh = form["NoiSinh"];
            cb.QueQuan = form["QueQuan"];
            cb.IDDanToc = Convert.ToInt32(form["IDDanToc"]);
            //end tab1
            cb.IDTonGiao = Convert.ToInt32(form["IDTonGiao"]);
            cb.HoKhauThuongTru = form["HoKhauThuongTru"];
            cb.DiaChi = form["DiaChi"];
            cb.TrangThai = Convert.ToInt32(form["TrangThai"]);
            //cb.NNTuyenDung = form["NNTuyenDung"];
            //cb.NgayTuyenDung = DateTime.Parse((form["NgayTuyenDung"] == "" ? "1/1/1900" : form["NgayTuyenDung"]),ci.DateTimeFormat);
            //cb.HinhThucTuyenDung = form["HinhThucTuyenDung"];
            //cb.CoQuanTuyenDung = form["CoQuanTuyenDung"];
            //cb.ChucVuHienTai = Convert.ToInt32(form["ChucVuHienTai"]);
            //cb.CongViecDuocGiao = form["CongViecDuocGiao"];
            //end tab2

            //cb.MaNgachCC = form["MaNgachCC"];
            //cb.TenNgachCC = form["TenNgachCC"];
            //cb.BacLuong = Convert.ToInt32(form["BacLuong"] == "" ? "1" : form["BacLuong"]);
            //cb.HeSoLuong = Convert.ToDouble(form["HeSoLuong"] == "" ? "0" : form["HeSoLuong"]);
            //cb.NgayHuongLuong = DateTime.Parse((form["NgayHuongLuong"] == "" ? "1/1/1900" : form["NgayHuongLuong"]),ci.DateTimeFormat);
            //cb.TrinhDoPhoThong = form["TrinhDoPhoThong"];
            //cb.TrinhDoChuyenMon = Convert.ToInt32(form["TrinhDoChuyenMon"]);
            //cb.ChuyenNganh = form["ChuyenNganh"];
            //cb.TrinhDoQLNN = Convert.ToInt32(form["TrinhDoQLNN"]);
            //cb.TrinhDoLyLuan = Convert.ToInt32(form["TrinhDoLyLuan"]);
            //end tab3

            //cb.TrinhDoNgoaiNgu = Convert.ToInt32(form["TrinhDoNgoaiNgu"]);
            //cb.TenNgoaiNgu = form["TenNgoaiNgu"];
            //cb.TrinhDoTinHoc = Convert.ToInt32(form["TrinhDoTinHoc"]);
            if (form["NgayVaoDang"] == "")
            {
                cb.NgayVaoDang.Equals("01/01/1900");
            }
            else
            {
                cb.NgayVaoDang = DateTime.Parse(form["NgayVaoDang"],ci.DateTimeFormat);
            }

            if (form["NgayVaoChinhThuc"] == "")
            {
                cb.NgayVaoChinhThuc.Equals("01/01/1900");
            }
            else
            {
                cb.NgayVaoChinhThuc = DateTime.Parse(form["NgayVaoChinhThuc"],ci.DateTimeFormat);
            }

            if (form["NgayTGTC_CTXH"] == "")
            {
                cb.NgayTGTC_CTXH.Equals("01/01/1900");
            }
            else
            {
                cb.NgayTGTC_CTXH = DateTime.Parse(form["NgayTGTC_CTXH"],ci.DateTimeFormat);
            }

            cb.NhiemVuTrongToChuc = form["NhiemVuTrongToChuc"]; cb.QuanHam = form["QuanHam"];
            if (form["NgayNhapNgu"] == "")
            {
                cb.NgayNhapNgu.Equals("01/01/1900");
            }
            else
            {
                cb.NgayNhapNgu = DateTime.Parse(form["NgayNhapNgu"],ci.DateTimeFormat);
            }
            if (form["NgayXuatNgu"] == "")
            {
                cb.NgayXuatNgu.Equals("01/01/1900");
            }
            else
            {
                cb.NgayXuatNgu = DateTime.Parse(form["NgayXuatNgu"],ci.DateTimeFormat);
            }
            //end tab4
            cb.DanhHieu = form["DanhHieu"];
            cb.NamPhongTang = form["NamPhongTang"];
            cb.SoTruongCongTac = form["SoTruongCongTac"];
            cb.TinhTrangSucKhoe = form["TinhTrangSucKhoe"];
            cb.ChieuCao = form["ChieuCao"];
            cb.CanNang = form["CanNang"];
            cb.NhomMau = form["NhomMau"];
            cb.ThuongBinh = form["ThuongBinh"];
            cb.GiaDinhChinhSach = form["GiaDinhChinhSach"];
            cb.SoCMND = form["SoCMND"];
            //end tab5
            cb.SoSoBHXH = form["SoSoBHXH"];
            cb.Tgiacapuyxa = form["Tgiacapuyxa"];
            cb.TgiaHDND = form["TgiaHDND"];
            cb.NgayCap = DateTime.Parse((form["NgayCap"] == "" ? "1/1/1900" : form["NgayCap"]),ci.DateTimeFormat);
            cb.NhanXet = form["NhanXet"];
            //cb.LoaiHinhDaoTao = Convert.ToInt32(form["LoaiHinhDaoTao"]);
            if (form["NgayNhanChuc"] == "")
            {
                cb.NgayNhanChuc.Equals("01/01/1900");
            }
            else
            {
                cb.NgayNhanChuc = DateTime.Parse(form["NgayNhanChuc"],ci.DateTimeFormat);
            }
            cb.TiengDanToc = form["TiengDanToc"];
            cb.TgiaQuocPhong = form["TgiaQuocPhong"];
            cb.ChucVuKiemNhiem = form["ChucVuKiemNhiem"];

            
            CanBoModels.Update(cb);
            return RedirectToAction("Index", "Pers", new { id = cb.IDPhongBan });
        }
        public ActionResult Delete(int id)
        {
            CanBo cb = CanBoModels.GetChiTietCanBo(id);
            TrinhDoLyLuanModels.DeleteAllOfPerson(id);
            TrinhDoQLNNModels.DeleteAllOfPerson(id);
            TrinhDoTinHocModels.DeleteAllOfPerson(id);
            TrinhDoNgoaiNguModels.DeleteAllOfPerson(id);
            TrinhDoChuyenMonModels.DeleteAllOfPerson(id);
            ChiTietPhuCapModels.DeleteAllOfPerson(id);
            QuaTrinhCongTacModels.DeleteAllOfPerson(id);
            DaoTaoModels.DeleteAllOfPerson(id);
            QHGiaDinhModels.DeleteAllOfPerson(id);
            KhenThuongModels.DeleteAllOfPerson(id);
            KyLuatModels.DeleteAllOfPerson(id);
            CanBoModels.Delete(id);
            return Redirect(Request.UrlReferrer.ToString());
            //RedirectToAction("Index", "Pers", new { id = cb.IDPhongBan });
        }

        public double getHeSoLuong(string mancc, int bacluong)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var result = (from nncc in context.DSNhomNgach
                         from mncc in context.DSMaNgach
                         where nncc.ID == mncc.IDNhomNgach && mncc.MaNgach==mancc
                         select nncc).First();
            if (bacluong <= result.SoBac)
            {
                return (result.HeSoGoc + (result.HeSoTang * (bacluong - 1)));
            }
            else return 0;
        }
    }
}
